package com.bigerdranch.android.geoquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 显示答案操作
 */

public class CheatActivity extends AppCompatActivity {
    private TextView mAnswerTextView;
    private Button mShowAnswerButton;

    public static final String EXTRA_ANSWER_IS_TRUE =
            "com.bignerdranch.android.geoquiz.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN =
            "com.bignerdranch.android.geoquiz.answer_shown";

    public static Intent newIntent(Context packageContext,String questionAndAnswer){
        Intent intent = new Intent(packageContext, CheatActivity.class);
        intent.putExtra(EXTRA_ANSWER_IS_TRUE,questionAndAnswer);
        return intent;
    }

    /**
     * 告诉MainActivity,用户是否查看了答案
     */
    public static boolean wasAnswerShown(Intent result){
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        // 获取extra信息
        mAnswerTextView = (TextView) findViewById(R.id.answer_text_view);
        mShowAnswerButton = (Button) findViewById(R.id.show_answer_button);
        // 显示按钮事件
        mShowAnswerButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //获得intent引用
                Intent intent = getIntent();
                //根据KEY取出value
                String message = intent.getStringExtra(EXTRA_ANSWER_IS_TRUE);
                //获得文本框引用，设置文字
                mAnswerTextView.setText(message);
                // 设置返回值告诉用户确实查看了答案
                setAnswerShowResult(true);

//                finish();
            }
        });
    }

    private void setAnswerShowResult(boolean isAnswerShown) {
        System.out.println(isAnswerShown);
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown);
        setResult(RESULT_OK,data);
    }
}